global without sharing class ApplicationForUnqualifiedDeliveHandler {

	public static void sendTaskToOwner(List<ApplicationForUnqualifiedDelive__c> list_new ){
		List<Task> tList = new List<Task>();
		Map<Id,String>accMap = new Map<Id,String>();
		for (ApplicationForUnqualifiedDelive__c afd : list_new) {
			accMap.put(afd.Account__c,null);
		}
		for(Account acc: [select Id,Name from Account where Id in: accMap.keySet()]){
			accMap.put(acc.Id,acc.Name );
		}
		
		for (ApplicationForUnqualifiedDelive__c afd : list_new) {
			 Task ta = new Task();
            ta.OwnerId = afd.OwnerId;
            ta.WhatId = afd.Id ;
            ta.Account__c  = afd.Account__c;
            ta.Subject =  accMap.get(afd.Account__c) + '客户的'+afd.Year__c+afd.Quarter__c+'的迈克任务量未达标!!!';
            ta.ActivityDate = System.today();
            ta.Description = accMap.get(afd.Account__c) + '客户的'+afd.Year__c+afd.Quarter__c+'的迈克任务量未达标，若为经销商客户NC系统已限制发货，请尽快完成任务量未达标发货申请单的填写，提交审批成功后，NC系统才能解除限制，完成后可关闭此任务通知！';
            tList.add(ta);
		}
		insert tList;
	}

	public static void backNc(List<ApplicationForUnqualifiedDelive__c> list_new ,Map<Id,ApplicationForUnqualifiedDelive__c>OldMap){
			
			
			Set<Id> appSets = new Set<Id>();
			for(ApplicationForUnqualifiedDelive__c ap: list_new){
				ApplicationForUnqualifiedDelive__c old = OldMap.get(ap.Id);

			
				if(ap.Status__c == '审批成功' && old.Status__c != ap.Status__c){

					appSets.add(ap.Id);
				}
				
			}
			if(appSets.isEmpty()) return;
			Map<String,String>companyMap = ImportObject.getPicklistValues('ApplicationForUnqualifiedDelive__c', 'Company__c', true);
			
			for(ApplicationForUnqualifiedDelive__c ap: [select NCICCode__c, Company__c, Id,Account__c,Account__r.WhetherOpenBusiness__c,Account__r.IfMoreCompany__c   from ApplicationForUnqualifiedDelive__c where Id in:appSets  ]){
				
			
				GetUnfinishAssignTaskUtil.sycnUnlockNC(ap.Company__c ,ap.NCICCode__c ,ap.Id );



			}




	}


	WebService static String  backToNc(String apid ){

	
			 ApplicationForUnqualifiedDelive__c ap = [select Status__c, NCICCode__c, Company__c,Account__r.System__c, Account__r.IfMoreCompany__c  , Id,Account__c,Account__r.WhetherOpenBusiness__c,
			 Account__r.RecordType.DeveloperName  
			 	from ApplicationForUnqualifiedDelive__c where Id =: apid 
				 ];
				
				if(ap.Status__c != '审批成功' ) return '未审批,不能同步!';

				Map<String,String>companyMap = ImportObject.getPicklistValues('ApplicationForUnqualifiedDelive__c', 'Company__c', true);
				
				return GetUnfinishAssignTaskUtil.asycnUnlockNC(ap.Company__c,ap.NCICCode__c ,apid);
			


	}
    
}